/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.sml.SEFM.controller;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.Iterator;
import javax.persistence.RollbackException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.SystemException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.SimpleFormController;
import vn.com.sml.SEFM.BUS.BankBUS;
import vn.com.sml.SEFM.BUS.CardBUS;
import vn.com.sml.SEFM.BUS.CardBlackBUS;
import vn.com.sml.SEFM.BUS.CitiesBUS;
import vn.com.sml.SEFM.BUS.IPBUS;
import vn.com.sml.SEFM.BUS.IPBlackBUS;
import vn.com.sml.SEFM.BUS.MerchantBUS;
import vn.com.sml.SEFM.BUS.PersonBUS;
import vn.com.sml.SEFM.BUS.fraudStandardBUS;
import vn.com.sml.SEFM.domain.Merchant;
import vn.com.sml.SEFM.domain.Ip;
import vn.com.sml.SEFM.domain.Banks;
import vn.com.sml.SEFM.domain.Card;
import vn.com.sml.SEFM.domain.Person;
import vn.com.sml.SEFM.domain.Transactions;
import vn.com.sml.SEFM.BUS.transactionBUS;
import vn.com.sml.SEFM.domain.Cardblacklist;
import vn.com.sml.SEFM.domain.Cities;
import vn.com.sml.SEFM.domain.Countries;
import vn.com.sml.SEFM.domain.Fraudstandard;
import vn.com.sml.SEFM.domain.Ipblacklist;


/** 
 *
 * @author Tunp
 */
public class listTransactionController extends SimpleFormController implements Controller {
    private transactionBUS transBUS = new transactionBUS();
    private MerchantBUS dvcntBUS = new MerchantBUS();
    private IPBUS ipBUS = new IPBUS();
    private IPBlackBUS ipblackBUS = new IPBlackBUS();
    private CardBUS cardBUS = new CardBUS();
    private CardBlackBUS cardblackBUS = new CardBlackBUS();
    private BankBUS bankBUS = new BankBUS();
    private fraudStandardBUS fraudBUS = new fraudStandardBUS();
    private PersonBUS personBUS = new PersonBUS();
    private CitiesBUS cityBUS = new CitiesBUS();

    public void setCityBUS(CitiesBUS cityBUS) {
        this.cityBUS = cityBUS;
    }

    public void setPersonBUS(PersonBUS personBUS) {
        this.personBUS = personBUS;
    }

    public void setBankBUS(BankBUS bankBUS) {
        this.bankBUS = bankBUS;
    }

    public void setCardBUS(CardBUS cardBUS) {
        this.cardBUS = cardBUS;
    }

    public void setCardblackBUS(CardBlackBUS cardblackBUS) {
        this.cardblackBUS = cardblackBUS;
    }

    public void setDvcntBUS(MerchantBUS dvcntBUS) {
        this.dvcntBUS = dvcntBUS;
    }

    public void setFraudBUS(fraudStandardBUS fraudBUS) {
        this.fraudBUS = fraudBUS;
    }

    public void setIpBUS(IPBUS ipBUS) {
        this.ipBUS = ipBUS;
    }

    public void setIpblackBUS(IPBlackBUS ipblackBUS) {
        this.ipblackBUS = ipblackBUS;
    }

    public void setTransBUS(transactionBUS transBUS) {
        this.transBUS = transBUS;
    }
     
    
    public listTransactionController() {
        //Initialize controller properties here or 
        //in the Web Application Context

//        setCommandClass(Transactions.class);
//        setCommandName("trans");
//        setSuccessView("successView");
//        setFormView("addTransaction");
    }
    
    @Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
     
        String listtrans = arg0.getParameter("listtranstype");
        String detail = arg0.getParameter("detailId");
        
        ModelAndView mv = new ModelAndView("listTransaction");
        
        if(detail != null){
            ModelAndView mv_detail = new ModelAndView("DetailTransaction");
            Transactions transac = transBUS.getTransactionById(Integer.parseInt(detail));
            List<Fraudstandard> lstFraud = new ArrayList<Fraudstandard>();
            String[] lst = transac.getFraudStandardCheck().split(",");
            for(int i=1;i<lst.length;i++)
            {
                int id = Integer.parseInt(lst[i]);
                Fraudstandard fr = fraudBUS.getFraudStandardById(id);
                lstFraud.add(fr);
            }
            mv_detail.addObject("transac", transac);
            mv_detail.addObject("lstFraud", lstFraud);
            return mv_detail;
        }
        
        List<Transactions> lstTrans = null;
        char mode = 'N'; // mode normal:N ; mode fraud:F;
        if(listtrans!= null && listtrans.equals("fraud")) {
            lstTrans = transBUS.listTransactions("isFraud=true");
            mode = 'F';
        }
        else {
            lstTrans = transBUS.listTransactions();
        }
        
        mv.addObject("lstTrans", lstTrans);
        mv.addObject("transmode",mode);
        return mv;
    }  
    
//    
//        @Override
//    protected ModelAndView onSubmit(
//    HttpServletRequest request, 
//    HttpServletResponse response, 
//    Object command, 
//    BindException errors) throws Exception {
//        ModelAndView mv = new ModelAndView(getSuccessView());
//        //Do something...
//        return mv;
//    }
}
